﻿// Copyright (c) 2015, Microsoft Corporation
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//

using System;
using System.Reflection;
using MyLivingRoom.Controls;

namespace MyLivingRoom.ViewModels
{
    public abstract class TopicDefinition
    {
        public string Id { get; set; }

        public string DisplayName { get; set; }

        public IconDefinition IconDefinition { get; set; }

        public Type TopicViewModelType
        {
            get { return _topicViewModelType; }
            set
            {
                if (value == null)
                    throw new ArgumentNullException(nameof(value));
                if (_topicViewModelType != null)
                    throw new InvalidOperationException($"Can only set {this.TopicViewModelType} once.");
                if (!typeof(ITopicViewModel).IsAssignableFrom(value))
                    throw new ArgumentException($"Specified type does not implement interface {nameof(ITopicViewModel)}: {value.FullName}");

                this.TopicViewModel = Activator.CreateInstance(_topicViewModelType = value);
                ((ITopicViewModel)this.TopicViewModel).TopicDefinition = this;

                _topicViewModelType = value;
            }
        }
        private Type _topicViewModelType;

        public object TopicViewModel
        {
            get { return _topicViewModel; }
            private set { _topicViewModel = value; }
        }
        private object _topicViewModel;

        public abstract void SelectTopic();
    }
}
